package business.manage.dictionary;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class StaticDictionary {
	@Autowired
	private DictionaryService dictionaryService;
	

	public static Map<String, List<Dictionary>> dictMap = new HashMap<String, List<Dictionary>>();

	public static List<Dictionary> dictList = new ArrayList<Dictionary>();
	


	@PostConstruct
	public void initDictMap()throws Exception{
		dictMap.clear();
		dictList.clear();
		this.initDictList();
		if (dictList != null && dictList.size() > 0) {
			for (int i = 0; i < dictList.size(); i++) {
				Dictionary dict = dictList.get(i);
				if (dict.getPid().equals("0") || dict.getPid().equals("")) {
					List<Dictionary> childDictList = new ArrayList<Dictionary>();
					for (int j = 0; j < dictList.size(); j++) {
						Dictionary childDict = dictList.get(j);
						if (childDict.getPid().equals(dict.getId())) {
							childDictList.add(childDict);
						}
					}
					dictMap.put(dict.getCode(), childDictList);
				}
			}
		}
	}
	

	public synchronized void initDictList() throws Exception{

		dictList = dictionaryService.listAllDictionary();
	}
	

	public static List<Dictionary> get(String key) {
		return dictMap.get(key);
	}
	
}
